package me.sealer.demo;

/**
 * Created by sealer on 17/05/17.
 */
public class DeadLock {
    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    public static void startThread(int flag) throws InterruptedException {
        if (1 == flag) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (obj1) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        synchronized (obj2) {
                            System.out.println("有1要2");
                        }
                    }
                }
            }).start();
        } else if (2 == flag) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (obj2) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        synchronized (obj1) {
                            System.out.println("有2要1");
                        }
                    }
                }
            }).start();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        DeadLock.startThread(1);
        DeadLock.startThread(2);
    }
}
